package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonArray;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.YMTDateUtil;

import cn.ymatou.admin.job.parameter.JobTaskitemListBean;
import cn.ymatou.admin.job.service.JobTaskitemListCall;
import net.sf.json.JSONObject;

public class JobQueryCallService {

	/**prodstock.iapi.ymatou.com商品ActivityProductStart
	 * 查询3天内的预约记录
	 * @param productid
	 * @param aid 活动id
	 * @param status START or END
	 * 请求报文:_search=true&size=10&appId=ProductStock&queueCode=ActivityProductStartOrEnd&startTime=2020-02-18+11%3A08%3A33&endTime=2020-02-19+11%3A18%3A33&bizId=p3286495-203032-START

	 * @return
	 */
	public static JobTaskitemListBean initActivityProductStartOrEnd(String productid,Integer aid,String status){
		JobTaskitemListBean MessageListBean=new JobTaskitemListBean();
		MessageListBean.set_search(true);
		MessageListBean.setTaskName("ProductStock.ActivityProductStartOrEnd");
		//userid_code
		if(productid!=null&&productid.length()>0){
			MessageListBean.setBizId(productid+"-"+aid+"-"+status);
		}
		MessageListBean.setEndTime(YMTDateUtil.getBeforeOrNextMunite(60*24*3));
		MessageListBean.setStartTime(YMTDateUtil.getBeforeOrNextMunite(-10));
		MessageListBean.setSize(10);
		return MessageListBean;
	}
	
	/**
	 * 获取job消息 list
	 * 
	 * @param MessageListBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static List <Map> GetJobTasks(JobTaskitemListBean MessageListBean) throws ClientProtocolException, IOException{
		List <Map> mqmsgs=new ArrayList<Map>();
		JobTaskitemListCall MessageListCall=new JobTaskitemListCall();
		MessageListCall.setData(MessageListBean);
		MessageListCall.callService();
		JsonArray joa=new JsonArray();
		try{
			joa=MessageListCall.getJsonObject().get("content").getAsJsonObject().get("content").getAsJsonArray();
			joa.forEach(ele->{
				mqmsgs.add((Map)FormatUtil.GSONToObject(ele.toString(), Map.class));
			});
		}catch (Exception e) {
			e.printStackTrace();
			Logger.debug("mq not found:"+FormatUtil.beanToGSONString(MessageListBean));
		}
		return mqmsgs;
	}
	
	public static void main(String args[]) throws ClientProtocolException, IOException, InterruptedException {
		 JobTaskitemListBean MessageListBean = JobQueryCallService.initActivityProductStartOrEnd("p3286495",203032,"START");
		List <Map> joa = JobQueryCallService.GetJobTasks(MessageListBean);
		if(joa.size()==0){
			Thread.sleep(5000);
			 joa = JobQueryCallService.GetJobTasks(MessageListBean);
		}
		joa.forEach(ele->{
			Logger.debug(ele.toString());
		});
		
	}
}
